
package com.sausageking.client.merchant.activity;

import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.inject.Inject;
import com.sausageking.client.MerchantServiceAsync;
import com.sausageking.client.mvp.BaseActivity;
import com.sausageking.client.merchant.place.StickerPlace;
import com.sausageking.client.merchant.ui.MerchantSignupView;
import com.sausageking.shared.Merchant;

public class MerchantSignupActivity extends BaseActivity
    implements
      MerchantSignupView.Presenter {

  @Inject
  private MerchantSignupView view;

  @Inject
  private PlaceController placeController;

  @Inject
  private MerchantServiceAsync service;

  @Inject
  private StickerPlace stickerPlace;

  @Override
  public String mayStop() {
    return null;
  }

  @Override
  public void submit() {
    Merchant merchant = view.getMerchant();
    if (!merchant.hasId()) {
      setToken(String.valueOf(merchant.getId()));
      service.insert(merchant, new AsyncCallback<Merchant>() {

        @Override
        public void onFailure(Throwable caught) {
          Window.alert("error!!!" + caught.getMessage());
        }

        @Override
        public void onSuccess(Merchant merchant) {
          view.setMerchant(merchant);
        }
      });
    } else {
      service.update(merchant, new AsyncCallback<Merchant>() {

        @Override
        public void onFailure(Throwable caught) {
          Window.alert("error!!!" + caught.getMessage());
        }

        @Override
        public void onSuccess(Merchant merchant) {
          view.setMerchant(merchant);
        }
      });
    }
  }

  @Override
  public void search() {
    service.getById(Long.valueOf(view.getSearchId()),
        new AsyncCallback<Merchant>() {

          @Override
          public void onFailure(Throwable caught) {
            Window.alert("error!!!" + caught.getMessage());
          }

          @Override
          public void onSuccess(Merchant result) {
            setToken(String.valueOf(result.getId()));
            view.setMerchant(result);
          }
        });
  }

  @Override
  public void searchByEmail() {
    service.getByEmail(view.getSearchEmail(), new AsyncCallback<Merchant>() {

      @Override
      public void onFailure(Throwable caught) {
        Window.alert("error!!!" + caught.getMessage());
      }

      @Override
      public void onSuccess(Merchant result) {
        setToken(String.valueOf(result.getId()));
        view.setMerchant(result);
      }
    });
  }

  @Override
  public void goToStickerPlace() {
    stickerPlace.setToken(getToken());
    placeController.goTo(stickerPlace);
  }

  @Override
  protected void startActivity(AcceptsOneWidget container, EventBus eventBus) {
    view.setPresenter(this);
    container.setWidget(view.asWidget());
    view.setSearchId(String.valueOf(getToken()));
    search();
  }
}
